package ar.com.ford.it.fullEconomics.persistence.dao.query;

import java.sql.Timestamp;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import org.apache.commons.lang.StringUtils;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc02Coststvers;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class CostStructureVersionsFindQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,
                                       Object[] parameters) {
        Expression exp = null;
        Teesc02Coststvers costStructureVersion = (Teesc02Coststvers) parameters[0];
        String status = (String) parameters[1];
        String lastAppOnly = (String) parameters[2];
        String dateType = (String) parameters[3];
        Timestamp dateMin = (Timestamp) parameters[4];
        Timestamp dateMax = (Timestamp) parameters[5];
        String userActionType = (String) parameters[6];
        String userId = (String) parameters[7];
        Expression supplierExp = null;
        Expression commodityExp = null;
        Expression partExp = null;
        String supplierClass = null;
        String supplierCode = null;
        String partClass = null;
        String partPrefix = null;
        String partBase = null;
        String partSuffix = null;
        Integer commodityNumber = null;

        exp = builder.get("eesc02PlantCodeC").equal(
                costStructureVersion.getEesc02PlantCodeC());

        // ############# Supplier
//        supplierExp = builder.get("teesc01Coststruct").get("teesp04Supply")
//                .get("teess01Supplier");
        supplierExp = builder.get("teesc01Coststruct").get("teess01Supplier");        

        supplierClass = costStructureVersion.getTeesc01Coststruct()
                .getTeesp04Supply().getTeess01Supplier()
                .getEess01SupplierClassCodeC();
        if (supplierClass != null) {
            exp = exp.and(supplierExp.get("eess01SupplierClassCodeC")
                    .equalsIgnoreCase(supplierClass));
        } /*else {
            exp = exp.and(supplierExp.get("eess01SupplierClassCodeC")
                    .equalsIgnoreCase("F").or(
                            supplierExp.get("eess01SupplierClassCodeC")
                                    .equalsIgnoreCase("C")));
        }*/

        supplierCode = costStructureVersion.getTeesc01Coststruct()
                .getTeesp04Supply().getTeess01Supplier()
                .getEess01SupplierCodeC();
        if (StringUtils.isNotBlank(supplierCode)) {
            exp = exp.and(supplierExp.get("eess01SupplierCodeC")
                    .equalsIgnoreCase(supplierCode));
        }
        // ############# End Supplier

        // ############# Commodity
        commodityExp = builder.get("teesc01Coststruct").get("teesp02Commodity");

        commodityNumber = costStructureVersion.getTeesc01Coststruct()
                .getTeesp02Commodity().getEesp02CommodityNumberK();
        if (commodityNumber != null) {
            exp = exp.and(commodityExp.get("eesp02CommodityNumberK").equal(
                    commodityNumber));
        }
        // ############# End Commodity

        // ############# Part
        partExp = builder.get("teesc01Coststruct").get("teesp04Supply").get(
                "teesp01Part");

        partClass = costStructureVersion.getTeesc01Coststruct()
                .getTeesp04Supply().getTeesp01Part().getEesp01PartClassCodeC();
        partPrefix = costStructureVersion.getTeesc01Coststruct()
                .getTeesp04Supply().getTeesp01Part().getEesp01PartPrefixCodeC();
        partBase = costStructureVersion.getTeesc01Coststruct()
                .getTeesp04Supply().getTeesp01Part().getEesp01PartBaseCodeC();
        partSuffix = costStructureVersion.getTeesc01Coststruct()
                .getTeesp04Supply().getTeesp01Part().getEesp01PartSuffixCodeC();

        if (partClass != null) {
            exp = exp.and(partExp.get("eesp01PartClassCodeC").equal(partClass));
        }/* else {
            exp = exp.and(partExp.get("eesp01PartClassCodeC").equalsIgnoreCase(
                    "F").or(
                    partExp.get("eesp01PartClassCodeC").equalsIgnoreCase("C")));
        }*/
        if (StringUtils.isNotBlank(partPrefix)) {
            exp = exp.and(partExp.get("eesp01PartPrefixCodeC")
                    .equal(partPrefix));
        }
        if (StringUtils.isNotBlank(partBase)) {
            exp = exp.and(partExp.get("eesp01PartBaseCodeC").equal(partBase));
        }
        if (StringUtils.isNotBlank(partSuffix)) {
            exp = exp.and(partExp.get("eesp01PartSuffixCodeC")
                    .equal(partSuffix));
        }
        // ############# End Part

        // ############# Date
        if (dateMin != null) {
            if ("Creation".equalsIgnoreCase(dateType)) {
                exp = exp.and(builder.get("eesc02CreateS").greaterThanEqual(
                        dateMin));
            }
            if ("Proposal".equalsIgnoreCase(dateType)) {
                exp = exp.and(builder.get("eesc02CststvProposalS")
                        .greaterThanEqual(dateMin));
            }
            if ("Approval".equalsIgnoreCase(dateType)) {
                exp = exp.and(builder.get("eesc02CststvApprovalS")
                        .greaterThanEqual(dateMin));
            }
            if ("EffDateIn".equalsIgnoreCase(dateType)) {
                exp = exp.and(builder.get("eesc02CststvInDateY")
                        .greaterThanEqual(dateMin));
            }
        }
        if (dateMax != null) {
            if ("Creation".equalsIgnoreCase(dateType)) {
                exp = exp.and(builder.get("eesc02CreateS").lessThanEqual(
                        dateMax));
            } else if ("Proposal".equalsIgnoreCase(dateType)) {
                exp = exp.and(builder.get("eesc02CststvProposalS")
                        .lessThanEqual(dateMax));
            } else if ("Approval".equalsIgnoreCase(dateType)) {
                exp = exp.and(builder.get("eesc02CststvApprovalS")
                        .lessThanEqual(dateMax));
            } else if ("EffDateIn".equalsIgnoreCase(dateType)) {
                exp = exp.and(builder.get("eesc02CststvInDateY").lessThanEqual(
                        dateMax));
            }
        }
        // ############# End Date        

        // ############# Status
        if (status != null && !status.equalsIgnoreCase("-1")) {
            if ("Draft".equalsIgnoreCase(status)) {
                exp = exp.and(builder.get("eesc02CreateS").notNull().and(
                        builder.get("eesc02CststvProposalS").isNull()));
            } else if ("Proposed".equalsIgnoreCase(status)) {
                exp = exp.and(builder.get("eesc02CststvProposalS").notNull()
                        .and(builder.get("eesc02CststvApprovalS").isNull()));
            } else if ("Approved".equalsIgnoreCase(status)) {
                exp = exp.and(builder.get("eesc02CststvApprovalS").notNull());
            }
        }
        // ############# End Status

        // ############# User
        if (StringUtils.isNotBlank(userId)) {
            if ("Creation".equalsIgnoreCase(userActionType)) {
                exp = exp.and(builder.get("eesc02CreateUserC")
                        .equalsIgnoreCase(userId));
            } else if ("Proposal".equalsIgnoreCase(userActionType)) {
                exp = exp.and(builder.get("eesc02CststvProposalUserC")
                        .equalsIgnoreCase(userId));
            } else if ("Approval".equalsIgnoreCase(userActionType)) {
                exp = exp.and(builder.get("eesc02CststvApprovalUserC")
                        .equalsIgnoreCase(userId));
            }

        }
        // ############# End User

        return exp;
    }
}
